﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SevenInvaders.common;
//using SevenInvaders.MENU;

namespace SevenInvaders.LEVEL_JONATHAN
{
    class Level_Jonathan
    {
        JonathanPlayer jonathanPlayer;
        JonathanEnemy jonathanEnemy;
        Texture2D fundodetela;
        public static Texture2D jonathantiro;

        public Level_Jonathan(ContentManager Content, GameWindow Window)
        {
            jonathanPlayer = new JonathanPlayer(Content.Load<Texture2D>("LEVEL_JONATHAN/jonathan"), new Vector2(Window.ClientBounds.Width/2, Window.ClientBounds.Height), 1, Window, Content);
            //jonathanEnemy = new JonathanEnemy(Content.Load<Texture2D>("LEVEL_JONATHAN/inimigo"), new Vector2(Window.ClientBounds.Width/2, 0));
            fundodetela = Content.Load<Texture2D>("LEVEL_JONATHAN/background");
            jonathantiro = Content.Load<Texture2D>("LEVEL_JONATHAN/tiro");

            for (int i = 0; i < 5; i++)
            {
                JonathanEnemy.list.Add(new JonathanEnemy(Content.Load<Texture2D>("LEVEL_JONATHAN/inimigo"), new Vector2(120 * i, 120)));


                JonathanEnemy.list[i].currentIA = JonathanEnemy.IAType.goDownAndUp;
            }
        }

        public void Update(GameTime gameTime, KeyboardState teclado, KeyboardState teclado_anterior)
        {
            for (int i = 0; i < Shoot.list.Count; i++)
            {
                Shoot.list[i].Update(gameTime);

                if (Shoot.list[i].PositionY < -Shoot.list[i].Texture.Height)
                {
                    Shoot.list[i].Visible = false;
                }

                for (int j = 0; j < JonathanEnemy.list.Count; j++)
                {

                    if (Shoot.list[i].Collision.Intersects(JonathanEnemy.list[j].Collision)
                        && Shoot.list[i].Visible)
                    {
                        Shoot.list[i].Visible = false;
                        JonathanEnemy.list[j].Visible = false;
                    }

                }

            }

            for (int i = 0; i < JonathanEnemy.list.Count; i++)
            {
                JonathanEnemy.list[i].Update(gameTime);
            }

            for (int i = 0; i < JonathanEnemy.list.Count; i++)
            {
                if (!JonathanEnemy.list[i].Visible)
                {
                    JonathanEnemy.list.RemoveAt(i);
                }
            }

            for (int i = 0; i < Shoot.list.Count; i++)
            {
                if (!Shoot.list[i].Visible)
                {
                    Shoot.list.RemoveAt(i);
                }
            }

            jonathanPlayer.Update(gameTime, teclado, teclado_anterior);
            //jonathanEnemy.Update(gameTime);
            
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(fundodetela, Vector2.Zero, Color.White);

            for (int i = 0; i < Shoot.list.Count; i++)
            {
                Shoot.list[i].Draw(gameTime, spriteBatch);
            }
            jonathanPlayer.Draw(gameTime, spriteBatch);

            for (int i = 0; i < JonathanEnemy.list.Count; i++)
            {
                JonathanEnemy.list[i].Draw(gameTime, spriteBatch);
            }
        }


    }
}
